package com.ymttest.business.data;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.Test;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.cdfgeteway.parameter.ReceiveCouponReq;
import com.shop2cn.iapi.cdfgeteway.parameter.UserCouponListReq;
import com.shop2cn.iapi.cdfgeteway.service.ReceiveCouponCall;
import com.shop2cn.iapi.cdfgeteway.service.UserCouponListCall;
import com.shop2cn.iapi.member.parameter.CdfUserAuthorBean;
import com.shop2cn.iapi.member.service.CdfUserAuthorCall;
import com.ymt.utils.annotation.TestSuite;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.ZhongMianStgCallService;
import com.ymttest.utils.TestDataManager;

@TestSuite
public class ZhongMianSit {
	/**
	 * 中免领取电子券
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	@Test
	public void TestReceiveCoupon() throws ClientProtocolException, IOException {
		for (int i = 0; i < 1; i++) {
			try {
				Integer uid = ZhongMianStgCallService.createUser();
				String token = ZhongMianStgCallService.createToken(uid);
				String addressid = ZhongMianStgCallService.createAddress(uid);
				String mobile = TestDataManager.getPhoneNum("11");
				GodMemberService.bindMobile(ZhongMianStgCallService.SITCDFSHOP, uid, mobile);
				ZhongMianStgCallService.CreateCdfMember(uid, mobile);
				ZhongMianStgCallService.CdfUserAuthor(uid, ZhongMianStgCallService.SITCDFSHOP);
				TestDataManager.appendFileToLine("ZhongMianSitTest", uid + "," + token + "," + addressid);
				List<String> cdfcouponbatch = Arrays.asList("10BLD001030", "20BLD001035", "40BLD001032");
				for (int j = 0; j < 1; j++) {
					ZhongMianStgCallService.receiveCdfCoupon(uid, cdfcouponbatch);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

//		ReceiveCouponReq ReceiveCouponReq = new com.shop2cn.iapi.cdfgeteway.parameter.ReceiveCouponReq();
//		ReceiveCouponCall ReceiveCouponCall = new ReceiveCouponCall();
//		ReceiveCouponReq.setShopId(500041475l);
//		ReceiveCouponReq.setUserId(Long.valueOf(uid));
//		ReceiveCouponReq.setBatchIdList(Arrays.asList("10BLD001030","20BLD001035","40BLD001032"));
//		ReceiveCouponCall.setData(ReceiveCouponReq);
//		ReceiveCouponCall.callService();
	}

	/**
	 * 获取中免电子券
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	@Test
	public void TestGetCoupon() throws ClientProtocolException, IOException {
		TestDataManager.getLines("ZhongMianSitTest").forEach(line -> {
			try {
				String[] ss = line.split(",");
				Integer uid = Integer.valueOf(ss[0]);
				UserCouponListReq UserCouponListReq = new com.shop2cn.iapi.cdfgeteway.parameter.UserCouponListReq();
				UserCouponListCall UserCouponListCall = new UserCouponListCall();
				UserCouponListReq.setCouponState("UNUSED");
				UserCouponListReq.setPageIndex(1);
				UserCouponListReq.setPageSize(100);
				UserCouponListReq.setShopId(500041475);
				UserCouponListReq.setUserId(uid);
				UserCouponListCall.setData(UserCouponListReq);
				UserCouponListCall.callService();
				JsonArray ja = UserCouponListCall.getJsonObject().get("coupons").getAsJsonArray();
				ja.forEach(info->{
					String couponCode = info.getAsJsonObject().get("couponCode").getAsString();
					TestDataManager.appendFileToLine("ZhongMianConponSitTest", uid + "," + ss[1] + "," + ss[2]+ "," +couponCode );
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

	}
	@Test
	public void TestAuthZhongMianMember() throws ClientProtocolException, IOException {
		TestDataManager.getLines("ZhongMianSitTest").forEach(line -> {
			try {
				String[] ss = line.split(",");
				Integer uid = Integer.valueOf(ss[0]);
				CdfUserAuthorBean CdfUserAuthorBean=new CdfUserAuthorBean();
				CdfUserAuthorCall CdfUserAuthorCall=new CdfUserAuthorCall();
				CdfUserAuthorBean.setShopId(500041475);
				CdfUserAuthorBean.setUserId(uid);
				CdfUserAuthorCall.setData(CdfUserAuthorBean);
				CdfUserAuthorCall.callService();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

	}
}
